<?php

class Ynadvsearch_Widget_SearchResultsController extends Engine_Content_Widget_Abstract
{
	public function indexAction()
	{
		$values = Zend_Controller_Front::getInstance()->getRequest()->getParams();
		$this->view->values = $values;
		if (isset($values['submit']) && isset($values['query']) && $values['query'] != '') {
			$text = $values['query'];
			$this->view->query = $text;
		}
		elseif (isset($values['is_search']) && $values['is_search'] == 1 && isset($values['query']) && $values['query'] != '') {
			$text = $values['query'];
			$this->view->query = $text;
		}
		elseif (isset($values['phrase']) && $values['phrase'] != '') {
			$text = $values['phrase'];
			$this->view->query = $text;
		}
		//$text = $values['query'];
		if ($text == '') {
			$this->view->noSearchQuery = 1;
			return;
		}
		if (isset($values['search']) && $values['search'] != '' &&!(isset($values['listmods']) && $values['listmods'] != '')) {
			$search = $values['search'];
		}
		elseif (isset($values['listmods']) && $values['listmods'] != '') {
			$listmods = explode(',',$values['listmods']);
		}
		else {
			$search = 'all';
		}
		$this->view->search = $search;
		$searchModulesTable = new Ynadvsearch_Model_DbTable_Modules;
		if (isset($values['from']) && $values['from'] != '') {
			$from = $values['from'];
		}
		else {
			$from = '0';
		}
		if (isset($values['type']) && $values['type'] == 'ajax') {
			$search = $values['search'];
		}
		$limit = 10;
		$this->view->limit = $limit;
		$results = array();
		if ($search == 'all') {
			$type = $searchModulesTable->getAllEnabledTypes();
			$modules = $searchModulesTable->getEnabledModules();
			foreach ($modules as $module) {
				$moduleType = $searchModulesTable->getTypes($module->name);
				$search_results = $this->getResults($text, $moduleType, $from, $limit);
				if (count($search_results) > 0) {
					$results[$module->name] = $search_results;
				}
			}
		}
		/*elseif (@$listmods && count($listmods) > 0) {
			foreach ($listmods as $mod) {
				print_r($mod);die;
				$type = $searchModulesTable->getTypes($mod);
				$search_results = $this->getResults($text, $moduleType, $from, $limit);
				if (count($search_results) > 0) {
					$results['$mod'] = $search_results;
				}
			}
		}*/  
		else {
			$type = $searchModulesTable->getTypes($search);
			$this->view->mod = $search;
			$type = $searchModulesTable->getTypes($search);
			$search_results = $this->getResults($text, $type, $from, $limit);
			if (count($search_results) > 0) {
				$results[$search] = $search_results;
			}
			if (count($search_results) < 11) {
				$this->view->endResult = 1;
			}
		}
		if (@$listmods && count($listmods) > 0 && $search != 'all') {
			$results = array();
			foreach ($listmods as $mod) {
				$moduleType = $searchModulesTable->getTypes($mod);
				$search_results = $this->getResults($text, $moduleType, $from, $limit);
				if (count($search_results) > 0) {
					$results[$mod] = $search_results;
				}
			}
		}  
		$this->view->from = $from;
		if (count($results) <= 0) {
			$this->view->noResult = 1;
			$this->view->endResult = 1;
		}
		$this->view->results = $results;
	}
	
	public function getResults($text, $types = null, $from, $limit) {
		$table = Engine_Api::_()->getDbtable('search', 'core');
		$table_name = $table->info('name');
		$db = $table->getAdapter();
		$select = $table->select();
		$text = '%'.preg_replace('/\s+/', '%',$text).'%';
		$availableTypes = Engine_Api::_()->getItemTypes();
		$types = array_intersect($types,$availableTypes);
		$types = array_diff($types, array("groupbuy_category", "groupbuy_location","groupbuy_param","groupbuy_album","groupbuy_photo","music_playlist_song"));
		$types = "('". implode("','", $types). "')";
		$select->where("$table_name.type IN ".  $types);
		$select->where("$table_name.title like '$text' OR $table_name.description like '$text'");
		$select->order("$table_name.type");
		$select->limit($limit + 1,$from);
		$results = $table->fetchAll($select);
		return $results;
	}
}
